package org.rency.crawler.plugins.service.impl;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import org.apache.commons.io.IOUtils;
import org.rency.crawler.plugins.charset.UniversalDetector;
import org.rency.crawler.plugins.service.UniversalDetectorPlugin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 描述: ${TODO}<br>
 * 最近修改时间:2017/11/24 11:47<br>
 *
 * @author CaiyuRen
 * @version V1.0
 * @since 2017/11/24 11:47
 */
@Component
public class UniversalDetectorPluginImpl implements UniversalDetectorPlugin {

  private static final Logger logger = LoggerFactory.getLogger(UniversalDetectorPluginImpl.class);

  private static final Integer BYTE_ARRAY_LENGTH = 4096;

  @Override
  public Charset detector(String filePath) {
    InputStream is = null;
    try {
      is = new FileInputStream(filePath);
      return this.detector(is);
    }catch (IOException e){
      logger.error("universal detector plugin error,and return default charset.",e);
      return Charset.defaultCharset();
    }finally {
      IOUtils.closeQuietly(is);
    }
  }

  @Override
  public Charset detector(byte[] datas) {
    return this.detector(new ByteArrayInputStream(datas));
  }

  private Charset detector(InputStream is){
    UniversalDetector detector = new UniversalDetector(null);
    try{
      byte[] buf = new byte[BYTE_ARRAY_LENGTH];
      int nread;
      while ((nread = is.read(buf)) > 0 && !detector.isDone()) {
        detector.handleData(buf, 0, nread);
      }
      detector.dataEnd();
      String charset = detector.getDetectedCharset();
      return Charset.forName(charset);
    }catch(IOException e){
      logger.error("universal detector plugin error,and return default charset.",e);
      return Charset.defaultCharset();
    }finally {
      IOUtils.closeQuietly(is);
      detector.reset();
    }
  }

}
